package com.example.Object;

import com.example.Testclass.Student;

import java.util.Objects;

public class TestObject {
    public static void main(String[] args) {
        Student1 s = new Student1("张三", 23);
        System.out.println(s);  // com.example.Object.Student1@1e643faf
        System.out.println(s.toString());   // Student1{age=23, name='张三'}

        Student1 s1 = new Student1("张三", 23);
        System.out.println(s == s1);    // false
        // Object类中的equals方法,底层也是用==号比较地址值
        System.out.println(s.equals(s1));   // true
    }
}

class Student1 {
    private String name;
    private int age;

    public Student1() {
    }

    public Student1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student1{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student1 student1 = (Student1) o;
        return age == student1.age && Objects.equals(name, student1.name);
    }
}